#ifndef UTILS_H_HEADER_INCLUDED
#define UTILS_H_HEADER_INCLUDED

#define IMPL_SINGLETON(klass)           \
    template<typename T>                \
    T* Singleton<T>::__instance = NULL; \
    template<typename T>                \
    bool Singleton<T>::__created_by_singleton = false;  \

template<typename T>
class Singleton
{
private:
    static T*   __instance;
    static bool __created_by_singleton;
protected:
    virtual void Destroy(){}
    ;
    virtual void Create(){};
protected:
    Singleton()
    {
        if( !__created_by_singleton )
            throw "can't create object by subclass";
    };
private:
    Singleton(Singleton*){ assert(false); };
    Singleton(Singleton&){ assert(false); };
    Singleton& operator=(Singleton& a){ assert(false); };
public:
    static T* Instance()
    {
        if( !__instance )
        {
            __created_by_singleton = true;
            try
            {
                __instance = new T;
            }
            catch(...)
            {
                __created_by_singleton = false;
            }
            __created_by_singleton = false;
        }
        return __instance;
    };

    static T& InstanceRef()
    {
        return (*Instance());
    };

    ~Singleton()
    { 
        Destroy(); 
        delete __instance; 
    };
};

#endif // UTILS_H_HEADER_INCLUDED